package com.gwtintegration.smartrf.builder;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JParameterizedType;
import com.google.gwt.core.ext.typeinfo.JType;
import com.gwtintegration.smartrf.client.GenerateConverter;

public class JTypeUtils {

	private static final Set<String> PRIMITIVE_TYPES = new HashSet<String>(Arrays.asList(String.class.getName(), Integer.class.getName(),
			Long.class.getName(), Float.class.getName(), Double.class.getName(), Date.class.getName()));

	private static final Set<String> COLLECTION_TYPES = new HashSet<String>(Arrays.asList(Set.class.getName(), List.class.getName()));

	public static boolean isPrimitive(JType type) {
		JClassType classType = type.isClass();
		return classType != null && PRIMITIVE_TYPES.contains(classType.getQualifiedSourceName());
	}

	public static boolean isCollection(JType type) {
		JClassType classType = type.isClassOrInterface();
		return classType != null && COLLECTION_TYPES.contains(classType.getQualifiedSourceName());
	}

	public static boolean hasGenerateConverterAnnotation(JType type) {
		JClassType interfaceType = type.isInterface();
		return interfaceType != null && interfaceType.isAnnotationPresent(GenerateConverter.class);
	}

	public static JClassType getSingleParameterType(JParameterizedType type) {
		JClassType[] parameterTypes = type.getTypeArgs();

		if (parameterTypes != null && parameterTypes.length > 0) {
			return parameterTypes[0];
		}

		return null;
	}
}
